import Vue from "vue";
import Router from "vue-router";
import NProgress from "nprogress";
import { getRouters } from "@/util/util";
import "nprogress/nprogress.css";
import system from "./modules/system";
import writings from "./modules/writings";
import approval from "./modules/approval";
import websites from "./modules/websites";
// import { getMenuListApi } from "@/api/modules/menu.api";
// import { testRouterList } from "./test";
// import publish from './modules/publish'
// import routers from "@/router/routers";

NProgress.configure({ showSpinner: false });

Vue.use(Router);

export const routerList = [
  ...writings,
  ...approval,
  // ...publish,
  ...system,
  ...websites,
];

// console.log("testRouterList", testRouterList);
// export const a = filterAsyncRouter(testRouterList)
// console.log("aaaaa router", localGet("router"));

export const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/templates/login/index"),
  },
  {
    path: "/layout",
    component: () => import("@/layouts"),
    children: [
      ...getRouters(routerList),
      ...[
        {
          path: "/404",
          component: () => import("@/components/status/404.vue"),
        },
      ],
    ],
  },
];

const router = new Router({
  routes,
});

router.beforeEach((to, from, next) => {
  NProgress.start();
  if (to.matched.length === 0) {
    NProgress.done();
    next({
      path: "/404",
    });
  } else {
    next();
  }
  NProgress.done();
});

const VueRouterPush = Router.prototype.push;
Router.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch((err) => err);
};

const originalReplace = Router.prototype.replace;
Router.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch((err) => err);
};

export default router;
